package com.erd.datacollect.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.util.Base64;

public class DesUtil {
    private static final String DES = "DES";
    private static Logger logger= LoggerFactory.getLogger(DesUtil.class);
    /**
     * Description 根据键值进行加密
     * @param data requestBody.toString()
     * @param key 加密键 byte 数组(app_key)
     * @return
     * @throws Exception
     */
    public static String encrypt(String data, String key){
        try {
            byte[] bt = encrypt(data.getBytes("UTF-8"), key.getBytes("UTF-8"));
            byte[] encodedBytes = Base64.getEncoder().encode(bt);
            String encodedString = new String(encodedBytes,"UTF-8");
            return encodedString;
        } catch (Exception e) {
            logger.error("加密失败：{}",e);
        }
        return null;
    }
    /**
     * Description 根据键值进行解密
     * @param data 加密后的 requestBody
     * @param key 加密键 byte 数组(app_key)
     * @return
     * @throws IOException
     * @throws Exception
     */
    public static String decrypt(String data, String key){
        if (data == null)
            return null;
        try {
            byte[] decodedBytes = Base64.getDecoder().decode(data);
            byte[] bt = decrypt(decodedBytes,key.getBytes("UTF-8"));
            return new String(bt, "UTF-8");
        } catch (Exception e) {
            logger.error("解密失败:{}",e);
        }
        return null;
    }
    /**
     * Description 根据键值进行加密
     * @param data
     * @param key 加密键 byte 数组
     * @return
     * @throws Exception
     */
    private static byte[] encrypt(byte[] data, byte[] key) throws Exception {
        // 生成一个可信任的随机数源
        SecureRandom sr = new SecureRandom();
        // 从原始密钥数据创建 DESKeySpec 对象
        DESKeySpec dks = new DESKeySpec(key);
        // 创建一个密钥工厂，然后用它把 DESKeySpec 转换成 SecretKey 对象
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
        SecretKey securekey = keyFactory.generateSecret(dks);
        // Cipher 对象实际完成加密操作
        Cipher cipher = Cipher.getInstance(DES);
        // 用密钥初始化 Cipher 对象
        cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
        return cipher.doFinal(data);
    }
    /**
     * Description 根据键值进行解密
     * @param data
     * @param key 加密键 byte 数组
     * @return
     * @throws Exception
     */
    private static byte[] decrypt(byte[] data, byte[] key) throws Exception {
        // 生成一个可信任的随机数源
        SecureRandom sr = new SecureRandom();
        // 从原始密钥数据创建 DESKeySpec 对象
        DESKeySpec dks = new DESKeySpec(key);
        // 创建一个密钥工厂，然后用它把 DESKeySpec 转换成 SecretKey 对象
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
        SecretKey securekey = keyFactory.generateSecret(dks);
        // Cipher 对象实际完成解密操作
        Cipher cipher = Cipher.getInstance(DES);
        // 用密钥初始化 Cipher 对象
        cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
        return cipher.doFinal(data);
    }

    public static void main(String[] args){
        String sign="DBwx1iVugwgt0mPH7D5ARJznLt9gNvMsA48EgP9xkINb+V5PNT7lfzmmqsGjK2tOtLXxAsFQGfahm4JYzW0G+Q==";
        String key="default_tenement";
        String message=decrypt(sign,key);
        System.out.println(message);
    }
}